//
//  ActivitePage.m
//  Grappin
//
//  Created by apple-120 on 4/24/13.
//
//
#import "Activite.h"
/******** add RecentSOS *********/
#import "RecentSOS.h"
#import "UTranslator.h"
#import "AlertView.h"
#import "Synchronize.h"
#import "FacadeSOS.h"
#import "Globals.h"
#import "DTOSOS.h"
/******** end RecentSOS *********/

/******** add RecentAc *********/
#import "RecentActivity.h"
#import "FacadeMessage.h"
#import "DTOMessage.h"
#import "EntityMessage.h"
#import "UMenuPopover.h"
#import "UMenuItem.h"
/******** end RecentAc *********/

#import "ActivitePage.h"

@interface ActivitePage ()

/******** add RecentSOS *********/
@property (retain, nonatomic) RecentSOS * recentSOS;
@property (readwrite, nonatomic) int numDisplayingMessage;
@property (assign, nonatomic) DTOSOS * deletingSOS;
@property (retain, nonatomic) NSIndexPath * deletingIndexPath;

- (NSMutableArray *)findSOS;
- (void)removeSOSCommand;
/******** end RecentSOS *********/

@property (retain, nonatomic) Activite * activite;

/******** add RecentAc *********/
@property (retain, nonatomic) RecentActivity * recentActivity;
@property (readwrite, nonatomic) int numDisplayingMessageSMS;
@property (assign, nonatomic) DTOMessage * deletingMessage;
@property (retain, nonatomic) NSIndexPath * deletingIndexPathSMS;

- (void)displayMoreContactsSMS;
- (void)displayMoreButton:(int)pNumRecord;
- (NSMutableArray *)findContacts;
- (void)removeMessageCommand;
/******** end RecentAc *********/

@end

@implementation ActivitePage

@synthesize activite;

@synthesize recentSOS, deletingIndexPath, deletingSOS, numDisplayingMessage;

@synthesize recentActivity, numDisplayingMessageSMS, deletingMessage, deletingIndexPathSMS;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.translatorWatcher  =   [[UTranslator getInstance] watch:^{
        self.title  =   [[UTranslator getInstance] getWordForKey:@"RECENT_ACTIVITY_TITLE"];
    }];
    /******** add RecentSOS *********/
    
    recentSOS   =   [[RecentSOS alloc] initWithStyle:UITableViewStylePlain];
    recentSOS.view.frame    =   self.view.bounds;
    [SOSView addSubview:recentSOS.view];

    recentSOS.removeSOSBlock   =   ^(id pItem, NSIndexPath * pIndexPath)
    {
        self.deletingSOS    =   pItem;
        self.deletingIndexPath  =   pIndexPath;
        [self removeSOSCommand];
    };
    /******** end RecentSOS *********/
    
//    activite    = [[Activite alloc] initWithStyle:UITableViewStylePlain];
//    activite.view.frame = self.view.bounds;
//    [self.view addSubview:activite.view];
    
    
    /******** add RecentAc *********/
    
    recentActivity  =   [[RecentActivity alloc] initWithStyle:UITableViewStylePlain];
//    SMSView.frame = CGRectMake(0, recentSOS.view.frame.size.height, 320, recentActivity.view.frame.size.height);
    recentActivity.view.frame   =   self.view.bounds;
    [SMSView addSubview:recentActivity.view];
    
    recentActivity.removeMessageBlock   =   ^(id pItem, NSIndexPath * pIndexPath)
    {
        self.deletingMessage    =   pItem;
        self.deletingIndexPathSMS  =   pIndexPath;
        [self removeMessageCommand];
    };
    /******** end RecentAc *********/
}
/******** add RecentSOS *********/
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.numDisplayingMessage   =   0;
    [self.recentSOS setData:[self findSOS]];
    
    self.numDisplayingMessageSMS   =   0;
    @autoreleasepool {
        [self.recentActivity setData:[self findContacts]];
    }
}

- (void)removeAllReferences
{
    [super removeAllReferences];
    recentSOS.removeSOSBlock    =   nil;
    recentSOS.addMoreSOSBlock   =   nil;
    
    self.recentActivity.removeMessageBlock  =   nil;
    self.recentActivity.moreButtonBlock     =   nil;
}

- (void)syncDataSuccessfully:(id)pData
{
    if ([pData isKindOfClass:[NSNumber class]])
    {
        NSNumber * tNumber = pData;
        int tSyncData   =   [tNumber intValue];
        if (((tSyncData & kSyncSOS) > 0) || ((tSyncData & kSyncMessage) > 0))
            [self viewWillAppear:FALSE];
    }
}

// private functions

- (void)displayMoreContacts
{
    [self.recentSOS addMoreSOS:[self findSOS]];
}

- (void)displayMoreButton:(int)pNumRecord
{
    int tNumRecord = [[FacadeSOS getInstance] countSOSRecords];
    
    
    BOOL tIsDisplayMoreButton = FALSE;
    if (tNumRecord > pNumRecord && tNumRecord > kGlobalLimitNumRowMessage)
        tIsDisplayMoreButton = TRUE;
    
    if (tIsDisplayMoreButton)
    {
        moreButtonBlock tBlock = ^()
        {
            [self displayMoreContacts];
        };
        self.recentSOS.addMoreSOSBlock = tBlock;
    }
    
    self.recentSOS.isDisplayMoreButton   =   tIsDisplayMoreButton;
}

- (NSMutableArray *)findSOS
{
    NSMutableArray * tSOS = [[FacadeSOS getInstance] findSOSFromRecord:self.numDisplayingMessage numSelectedRow:kGlobalLimitNumRowMessage];
    self.numDisplayingMessage   +=  tSOS.count;
    [self displayMoreButton:self.numDisplayingMessage];
    return tSOS;
}

- (void)removeSOSCommand;
{
    [[AlertView instance] display:@"ALERT_DELETE_MESSAGE_TILE" messageKey:@"ALERT_DELETE_MESSAGE_MESSAGE" okeKey:@"OKE_BUTTON_TITLE" cancelKey:@"CANCLE_BUTTON_TITLE" onOkeBlock:^(id pData)
     {
         EntitySOS * tEntitySOS = [[FacadeSOS getInstance] findById:[NSString stringWithFormat:@"%i", self.deletingSOS.SOSId]];
         if (tEntitySOS)
         {
             [[FacadeSOS getInstance] remove:tEntitySOS];
             [self.recentSOS removeCellByIndexPath:self.deletingIndexPath];
             self.numDisplayingMessage--;
         }
         self.deletingSOS        =   nil;
         self.deletingIndexPath  =   nil;
     } onCancelBlock:^(id pData)
     {
         self.deletingSOS       =   nil;
         self.deletingIndexPath =   nil;
     }];
}


/******** end RecentSOS *********/


/******** add RecentAc *********/
/*
- (void)syncDataSuccessfully:(id)pData
{
    if ([pData isKindOfClass:[NSNumber class]])
    {
        NSNumber * tNumber = pData;
        int tSyncData   =   [tNumber intValue];
        if ((tSyncData & kSyncMessage) > 0)
            [self viewWillAppear:FALSE];
    }
}
*/
// private functions

- (void)displayMoreContactsSMS
{
    @autoreleasepool {
        [self.recentActivity addMoreContacts:[self findContacts]];
    }
}

- (void)displayMoreButtonSMS:(int)pNumRecord
{
    int tNumRecord = [[FacadeMessage getInstance] countContactsSendedAndReceivedMessage];
    
    BOOL tIsDisplayMoreButton = FALSE;
    if (tNumRecord > pNumRecord && tNumRecord > kGlobalLimitNumRowMessage)
        tIsDisplayMoreButton = TRUE;
    
    if (tIsDisplayMoreButton)
    {
        self.recentActivity.moreButtonBlock = ^()
        {
            [self displayMoreContactsSMS];
        };
    }
    self.recentActivity.isDisplayMoreButton   =   tIsDisplayMoreButton;
}

- (NSMutableArray *)findContacts
{
    NSMutableArray * tMessages = [[FacadeMessage getInstance] findMessagesGroupedByContactFromRecord:self.numDisplayingMessageSMS numSelectedRow:kGlobalLimitNumRowMessage];
    self.numDisplayingMessageSMS   +=  tMessages.count;
    [self displayMoreButtonSMS:self.numDisplayingMessageSMS];
    return tMessages;
}

- (void)removeMessageCommand
{
    [[AlertView instance] display:@"ALERT_DELETE_MESSAGE_TILE" messageKey:@"ALERT_DELETE_MESSAGE_MESSAGE" okeKey:@"OKE_BUTTON_TITLE" cancelKey:@"CANCLE_BUTTON_TITLE" onOkeBlock:^(id pData)
     {
         if (self.deletingMessage.groupId != kGlobalNoneGroupId)
             [[FacadeMessage getInstance] removeMessagesByGroupId:self.deletingMessage.groupId];
         else
             [[FacadeMessage getInstance] removeMessagesByContactId:self.deletingMessage.contactId];
         
         [self.recentActivity removeCellByIndexPath:self.deletingIndexPathSMS];
         self.deletingMessage    =   nil;
         self.deletingIndexPathSMS  =   nil;
         self.numDisplayingMessageSMS--;
     } onCancelBlock:^(id pData)
     {
         self.deletingMessage    =   nil;
         self.deletingIndexPathSMS  =   nil;
     }];
}

/******** end RecentAc *********/

- (void)dealloc
{
    [recentActivity.view removeFromSuperview];
    [recentActivity release];
    [deletingIndexPathSMS release];
    
    
    
    [recentSOS.view removeFromSuperview];
    [recentSOS release];
    [deletingIndexPath release];
    [SOSView release];
    [SMSView release];
    
    [activite release];
    [super dealloc];
}

- (void)viewDidUnload {
    [SOSView release];
    SOSView = nil;
    [SMSView release];
    SMSView = nil;
    [super viewDidUnload];
}
@end
